package com.ruoyi.quality.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Stream;

import cn.hutool.core.bean.BeanUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.quality.domain.TQualityFileItem;
import com.ruoyi.quality.domain.dto.TQualityBizEnterCheckDTO;
import com.ruoyi.quality.domain.dto.TQualityEnterCheckDTO;
import com.ruoyi.quality.domain.dto.TQualityPlanTaskDTO;
import com.ruoyi.quality.service.ITQualityEnterCheckService;
import com.ruoyi.quality.service.ITQualityFileItemService;
import com.ruoyi.quality.service.ITQualityFileService;
import com.ruoyi.quality.service.ITQualityPlanTaskService;
import com.ruoyi.safe.domain.*;
import com.ruoyi.safe.domain.dto.BizEnterCheckDTO;
import com.ruoyi.safe.domain.dto.EnterCheckDTO;
import com.ruoyi.safe.domain.dto.EntityIdAppDTO;
import com.ruoyi.safe.domain.dto.ProjectSearchDTO;
import com.ruoyi.safe.domain.vo.BizEnterCheckProjectVO;
import com.ruoyi.safe.domain.vo.CommonProjectVO;
import com.ruoyi.safe.mapper.*;
import com.ruoyi.safe.service.*;
import com.ruoyi.safe.service.impl.BizEnterCheckProjectServiceImpl;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysUserService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 计划任务明细Service业务层处理
 * 
 * @author mahl2
 * @date 2024-02-28
 */
@Service
public class TQualityPlanTaskServiceImpl implements ITQualityPlanTaskService
{
    @Value("${minio.bucketName}")
    private String bucketName;

    @Autowired
    private ITQualityFileService iTQualityFileService;

    @Autowired
    private BizExcludePlanTaskMapper bizExcludePlanTaskMapper;

    @Autowired
    private BizExcludePlanMapper bizExcludePlanMapper;

    /**
     * 查询计划任务列表
     * @param tQualityPlanTaskDTO
     * @return
     */
    @Override
    public List<TQualityPlanTaskDTO> selectPlanTaskList(TQualityPlanTaskDTO tQualityPlanTaskDTO)
    {
        List<TQualityPlanTaskDTO> listRet = new ArrayList<>();
        List<BizExcludePlanTask> list = selectPlanTaskListAll(tQualityPlanTaskDTO);
        for (BizExcludePlanTask item : list)
        {
            TQualityPlanTaskDTO item2 = new TQualityPlanTaskDTO();
            item2.setId(item.getId());
            item2.setTitle(item.getTitle());
            item2.setRemark(item.getRemark());
            item2.setStartDate(item.getStartDate());
            item2.setEndDate(item.getEndDate());
            item2.setExcludeFrequency(item.getExcludeFrequency());
            item2.setDateType(item.getDateType());
            item2.setDateTypeName(item.getDateTypeName());
            item2.setExcludeType(item.getExcludeType());
            item2.setPlanId(item.getPlanId());
            item2.setProjectId(item.getProjectId());
            item2.setUserName(item.getUserName());
            item2.setBucket(bucketName);

            // 创建一个DateTime对象
            Date dateTime = item.getCreateTime();
            // 定义日期格式化的模式
            String pattern = "yyyy-MM-dd HH:mm:ss";
            // 创建SimpleDateFormat对象并设置日期格式
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            // 调用format()方法将DateTime对象转换为字符串
            String strDateTime = sdf.format(dateTime);
            item2.setCreateTime(strDateTime);
            item2.setCurrentTime(item.getCurrentTime());
            item2.setQualityNum(item.getQualityNum());
            item2.setQualityOverNum(item.getQualityOverNum());
            listRet.add(item2);
        }
        return listRet;
    }
    /**
     * 查询计划任务列表
     * @param tQualityPlanTaskDTO
     * @return
     */
    @Override
    public List<BizExcludePlanTask> selectPlanTaskListAll(TQualityPlanTaskDTO tQualityPlanTaskDTO)
    {
        BizExcludePlanTask bizExcludePlanTask = new BizExcludePlanTask();
        bizExcludePlanTask.setId(tQualityPlanTaskDTO.getId());
        bizExcludePlanTask.setTitle(tQualityPlanTaskDTO.getTitle());
        bizExcludePlanTask.setRemark(tQualityPlanTaskDTO.getRemark());
        bizExcludePlanTask.setStartDate(tQualityPlanTaskDTO.getStartDate());
        bizExcludePlanTask.setEndDate(tQualityPlanTaskDTO.getEndDate());
        bizExcludePlanTask.setExcludeFrequency(tQualityPlanTaskDTO.getExcludeFrequency());
        bizExcludePlanTask.setExcludeType(tQualityPlanTaskDTO.getExcludeType());
        bizExcludePlanTask.setPlanId(tQualityPlanTaskDTO.getPlanId());
        bizExcludePlanTask.setProjectId(tQualityPlanTaskDTO.getProjectId());
        bizExcludePlanTask.setUserName(tQualityPlanTaskDTO.getUserName());
        if (tQualityPlanTaskDTO.getSelectType() == null || !tQualityPlanTaskDTO.getSelectType().equals("1"))
        {
            // 创建一个DateTime对象
            Date dateTime = DateUtils.getNowDate();
            // 定义日期格式化的模式
            String pattern = "yyyy-MM-dd HH:mm:ss";
            // 创建SimpleDateFormat对象并设置日期格式
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            // 调用format()方法将DateTime对象转换为字符串
            String strDateTime = sdf.format(dateTime);
            bizExcludePlanTask.setCurrentTime(strDateTime);
        }
        List<BizExcludePlanTask> list = bizExcludePlanTaskMapper.selectBizExcludePlanTaskList2(bizExcludePlanTask);
        return list;
    }

    /**
     * 拆分质量问题文件
     *
     * @param listFileItems 质量问题拆分文件
     * @return 结果
     */
    public int splitFile(List<TQualityFileItem> listFileItems)
    {
        String fileId = "";
        if (listFileItems.size() > 0)
        {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            for (TQualityFileItem item0 : listFileItems)
            {
                item0.setCheckPeopleId(loginUser.getUserId().toString());
                fileId = String.valueOf(item0.getFileId());
            }
            return iTQualityFileService.splitFileNew(listFileItems,fileId);
        }
        else
        {
            return 0;
        }
    }

}
